package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.*;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author karlfu
 * @since 2022/06/18 14:48
 */

@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private IKaptchaService iKaptchaService;

    @Autowired
    private ILoginService iLoginService;

    @PostMapping("/login")
    public ResponseData login(@RequestBody Map<String, String> map, HttpServletRequest request, HttpServletResponse response) {
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");

        // 验证码的确认
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);

        String code = kaptchaCodeResponse.getCode();
        if (!code.equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }

        // 验证用户名和密码
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(userName);
        userLoginRequest.setPassword(userPwd);
        UserLoginResponse userLoginResponse = iLoginService.login(userLoginRequest);

        // 若账号密码正确
        if (userLoginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            Cookie access_token = CookieUtil.genCookie("access_token", userLoginResponse.getToken(), "/", 3600);
            access_token.setHttpOnly(true);

            response.addCookie(access_token);
            return new ResponseUtil<>().setData(userLoginResponse);
        }

        // 若账号密码错误
        return new ResponseUtil<>().setErrorMsg(userLoginResponse.getMsg());
    }


    // 登录验证
    @GetMapping("/login")
    public ResponseData checkLogin( HttpServletRequest request){
        // 拿到当前登录用户的token，保存了uid和username信息
        String userInfo = request.getHeader("user_info");
        Object parse = JSON.parse(userInfo);
        return new ResponseUtil<>().setData(parse);
    }

    // 邮件激活
    @GetMapping("/verify")
    public ResponseData verify(String username,String uuid){
        // 邮件连接传送过来的username
        // 到登录验证表中查询uuid
        String realUuid = iLoginService.verify(username);
        // 再和邮件连接传送过来的uuid比对
        if (!realUuid.equals(uuid)){
            // 验证失败
            return new ResponseUtil<>().setErrorMsg(UserRetCode.USERVERIFY_INFOR_INVALID.getMessage());
        }
        // 验证成功
        // 修改用户验证表的verify, 修改用户表的verify
        UserVerifyResponse userVerifyResponse = iLoginService.verified(username);
        if (userVerifyResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(userVerifyResponse.getMsg());


    }


    @GetMapping("/loginOut")
    public ResponseData logout(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("access_token") ){
                    Cookie access_token = CookieUtil.genCookie("access_token", cookie.getValue(), "/", 0);
                    access_token.setHttpOnly(true);
                    response.addCookie(access_token);
                }
            }
        }
        return new ResponseUtil<>().setData(null);
    }
}
